//
//  HYServerViewController.m
//  HaiYue
//
//  Created by skyline on 2017/12/14.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYServerViewController.h"
#import "HYTypeViewController.h"
#import "HYServerList.h"
#import "HYTypeList.h"
#import "HYKaifuModel.h"
#import "HYGameTypeModel.h"
#import "HYKaifuOrder.h"
@interface HYServerViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) HYServerList *onlineList;
@property (nonatomic, strong) HYTypeList *typeList;
@property (nonatomic, strong) HYKaifuList *kaifuList;//model
@property (nonatomic, strong) HYGameTypeList *gametypeList;//model
@property (nonatomic, strong) NSMutableArray *list;
@end

@implementation HYServerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.style = HYViewNaviBarStyleSeg;
    self.segNaviBar.items = @[@"我要加入",@"搜索"];
    // Do any additional setup after loading the view.
}

- (HYServerList *)onlineList
{
    if (!_onlineList)
    {
        _onlineList = [[HYServerList alloc]init];
        _onlineList.mj_header = [HYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(actionForServerRefresh:)];
        [self.scrollView addSubview:_onlineList];
        [_onlineList mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView);
            make.left.equalTo(self.scrollView);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT));
        }];
    }
    return _onlineList;
}

- (HYTypeList *)typeList
{
    if (!_typeList)
    {
        _typeList = [[HYTypeList alloc]init];
        _typeList.mj_header = [HYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(actionForSearchRefresh:)];
        [self.scrollView addSubview:_typeList];
        [_typeList mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView);
            make.left.equalTo(self.scrollView).with.offset(SCREEN_WIDTH);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT));
        }];
    }
    return _typeList;
}

- (void)setKaifuList:(HYKaifuList *)kaifuList
{
    if (_kaifuList != kaifuList)
    {
        _kaifuList = kaifuList;
        self.onlineList.list = [HYKaifuOrder order:_kaifuList];
    }
}
- (void)setGametypeList:(HYGameTypeList *)gametypeList
{
    if (_gametypeList != gametypeList)
    {
        _gametypeList = gametypeList;
        self.typeList.list = [NSMutableArray arrayWithArray:self.gametypeList.list];
    }
}
- (void)actionForServerRefresh:(UITableView *)view
{
    [self serverRequest];
}
- (void)actionForSearchRefresh:(UITableView *)view
{
    [self searchRequest];
}
- (void)actionForServerCollectionCell:(NSNotification *)notify
{
    id vc = [NSClassFromString(@"HYGameInfoViewController") new];
    [vc setValue:[notify.userInfo valueForKey:@"message"] forKey:@"gameid"];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)actionForSearchItem:(NSNotification *)notify
{
    NSLog(@"===== %@",notify.userInfo);
    HYTypeViewController *VC = [[HYTypeViewController alloc] init];
    VC.name = [notify.userInfo valueForKey:@"name"];
    VC.prop = [notify.userInfo valueForKey:@"property"];
    [self.navigationController pushViewController:VC animated:YES];
}

//开服
- (void)serverRequest
{
    [self startHUD];
    //开服
    [CCHttp post:API_KAIFU_ONLINE para:nil success:^(id  _Nonnull response) {
        [self endHUD];
        [self.onlineList.mj_header endRefreshing];
        self.kaifuList = [HYKaifuList mj_objectWithKeyValues:response];
    } fail:^(NSError * _Nonnull error) {
        [self endHUD];
        [self.onlineList.mj_header endRefreshing];
    }];
    
//    [HYHttps post:API_KAIFU_ONLINE para:nil complete:^(id response) {
//        [self endHUD];
//        [self.onlineList.mj_header endRefreshing];
//        self.kaifuList = [HYKaifuList mj_objectWithKeyValues:response];
//    } fail:^(NSDictionary *dict) {
//        [self endHUD];
//        [self.onlineList.mj_header endRefreshing];
//    }];
}

//搜索
- (void)searchRequest
{
    [self startHUD];
    [CCHttp post:API_SEARCH para:nil success:^(id  _Nonnull response) {
        [self endHUD];
        [self.typeList.mj_header endRefreshing];
        self.gametypeList = [HYGameTypeList mj_objectWithKeyValues:response];
    } fail:^(NSError * _Nonnull error) {
        [self endHUD];
        [self.typeList.mj_header endRefreshing];
    }];
    
//    [HYHttps post:API_SEARCH para:nil complete:^(id response) {
//        [self endHUD];
//        [self.typeList.mj_header endRefreshing];
//        self.gametypeList = [HYGameTypeList mj_objectWithKeyValues:response];
//    } fail:^(NSDictionary *dict) {
//        [self endHUD];
//        [self.typeList.mj_header endRefreshing];
//    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [CCNotice addObs:self sel:@selector(actionForServerCollectionCell:) name:HYServerCollectionCelltemClick obj:nil];
    [CCNotice addObs:self sel:@selector(actionForSearchItem:) name:HYTypeCellItemClick obj:nil];
    [self serverRequest];
    [self searchRequest];
    
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [CCNotice rmObs:self name:HYServerCollectionCelltemClick obj:nil];
    [CCNotice rmObs:self name:HYTypeCellItemClick obj:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
